﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DHQGHN.DAL.BO.Admin;
using DHQGHN.DAL.Common;
using DHQGHN.PM0.Models.Admin;
using DHQGHN.PM0.Controllers;
using DHQGHN.PM0.Common.Attributes;
using DHQGHN.Common;

namespace DHQGHN.MVC.Controllers
{
    public class LogController : BaseController
    {
        private LogBO objBO;
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(LogController));
        public ActionResult Index(LogModel a = null, int pageIndex = 0)
        {
            try
            {
                SetActiveMenu("/Log/Index");
                a.Action = "";
                var model = Search(a, pageIndex);
                ViewBag.AppList = CommonData.getListApp();

                return View(model);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public ActionResult ViewDetail(long id)
        {
            try
            {
                CreateSqlBO();
                DAL.Models.Admin.Log item = objBO.GetById(id);
                LogModel model = new LogModel(item);

                return PartialView(model);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ActionDelete(string[] listID)
        {
            try
            {
                CreateSqlBO();
                bool result = objBO.Delete(listID);
                if (result)
                {
                    return Json(JsonResponse.Json200("Xóa thành công"));
                }
                else
                {
                    return Json(JsonResponse.Json500("Xảy ra lỗi trong quá trình xử lý"));
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public ActionResult ActionSearch(LogModel model = null, int pageIndex = 0)
        {
            try
            {
                var dataPage = Search(model, pageIndex);

                return PartialView("~/Views/Log/_LogList.cshtml", dataPage);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        private DataPage<LogModel> Search(LogModel model, int pageIndex)
        {
            try
            {
                CreateSqlBO();
                var DALModel = objBO.GetDynamicListPaging(model.ToDALModel(), pageIndex, PageSize);
                var models = new DataPage<LogModel>();
                var lstItem = DALModel.Data;
                foreach (DHQGHN.DAL.Models.Admin.Log item in lstItem)
                {
                    models.Data.Add(new LogModel(item));
                }

                if (models.Data.Count > 0)
                {
                    var pagNative = new Pagination
                    {
                        ActionName = "ActionSearch",
                        ModelName = "Log",
                        CurrentPage = pageIndex,
                        InputSearchId = "",
                        TagetReLoadId = "country-list"
                    };
                    pagNative.TotalRows = models.Data[0].RecordCount;
                    pagNative.CurrentRow = models.Data.Count;
                    ViewBag.Pagination = pagNative;
                }

                return models;
            }
            catch
            {
                throw;
            }
        }

        private void CreateSqlBO()
        {
            objBO = new LogBO();
        }
    }
}